#include "unp.h"

static void str_cli(FILE* stream, int sockfd){
	char	sendline[MAXLINE], recvline[MAXLINE];
	while(Fgets(sendline, MAXLINE, stream) != NULL){
		Writen(sockfd, sendline, strlen(sendline));
		if(Readline(sockfd, recvline, MAXLINE) == 0)
			err_quit("str_cli: server terminated prematurely");
		Fputs(recvline, stdout);
	}
}
int main(int argc, char** argv){
	int sockfd;
	struct sockaddr_in	servaddr;

	if(argc != 2)
		err_quit("usage: a.out <ip-address>");

	sockfd = Socket(AF_INET, SOCK_STREAM, 0);
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);
	if( inet_aton(argv[1], &servaddr.sin_addr) < 0)
		err_sys("inet_aton error");
	
	Connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr));

	str_cli(stdin, sockfd);
	exit(0);
}
